import 'package:flutter/material.dart';
import 'package:ichinesereader/routes/routePages.dart';
import 'package:ichinesereader/themes/pageStyles.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/forms/profile.dart';
import 'package:ichinesereader/controller/user.dart';
import 'package:get/get.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/cache.dart';
import 'package:ichinesereader/widgets/layouts/appdrawerTeacher.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';
import 'package:ichinesereader/widgets/sidebars/teacherSidebar.dart';

class TeacherProfile extends StatefulWidget {
  static const String routeName = '/teacher/profile';

  @override
  State<TeacherProfile> createState() => _TeacherProfileState();
}

class _TeacherProfileState extends State<TeacherProfile> {
  final box = Hive.box('appconfig');
  final UserController _userController = Get.find();

  @override
  void initState() {
    bool isTeacher = box.get(CacheManagerKey['nanhairole'].toString()) ==
        Constants.NanhaiRole_Teacher;
    if (isTeacher) {
      Future.delayed(Duration(milliseconds: 200), (() {
        _userController.getProfile();
      }));
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('build TeacherProfile');

    final isSmallScreen =
        MediaQuery.of(context).size.width < Constants.DefinedScreenSmallSize;
    final isBigScreen =
        MediaQuery.of(context).size.width > Constants.DefinedScreenBigSize;
    int _selectedSidebarItem = RoutePages.teacherPageItems
        .indexWhere((page) => page.route == Get.currentRoute);

    final PageStyles _pageStyles = PageStyles();

    return Scaffold(
        appBar: isSmallScreen ? CustomAppBar(title: 'Profile'.tr) : null,
        drawer: isSmallScreen ? TeacherAppDrawer() : null,
        body: Container(
          height: double.infinity,
          decoration: _pageStyles.pageBoxDecoration(),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Visibility(
                  visible: !isSmallScreen,
                  child: TeacherSidebarX(
                    selectedIndex: _selectedSidebarItem,
                    isBigScreen: isBigScreen,
                  )),
              Expanded(
                  child: Container(
                height: double.infinity,
                margin: _pageStyles.mainContainerPageEdgeInsets(),
                child: SingleChildScrollView(
                  primary: false,
                  child: GetX<UserController>(
                      init: UserController(),
                      builder: (_userController) {
                        return ProfileWidget(
                            profile: _userController.profile.value);
                      }),
                ),
              )),
            ],
          ),
        ));
  }
}
